package ListManager;

import java.util.ArrayList;

import model.Server;
import ui.DestinationServerActivity;
import utils.Tools;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ui.R;

public class DstServerListAdapter extends BaseAdapter implements OnClickListener {
		
	 private Activity activity;
	 private ArrayList<Server> data;
	 private static LayoutInflater inflater=null;
	 public Resources res;
	 private Server tempValues=null;
	 private int mSelectedItem=-1;
	 
	 public DstServerListAdapter(Activity a, ArrayList<Server> d,Resources resLocal) {   
	     activity = a;
	     data=d;
	     res = resLocal;
	     inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	 }
	 
	 public int getCount() {
		 if(data.size()<=0)
			 return 1;
		 
	         return data.size();
	 }
	 
     public Object getItem(int position) {
         return position;
     }
  
     public long getItemId(int position) {
         return position;
     }
     
     public static class ViewHolder{    
         public TextView name;
         public TextView state;
         public TextView dist;
         public TextView last;
     }
	  
	 public View getView(int position, View convertView, ViewGroup parent) {	      
	     View vi = convertView;
	     ViewHolder holder;
	     
	     if(convertView==null){
	        vi = inflater.inflate(R.layout.servertabitem, null);
	         
	        TextView nameTV = (TextView) vi.findViewById(R.id.serverNameTextView);
	        TextView stateTV = (TextView) vi.findViewById(R.id.serverStateTextView);
	        TextView distTV = (TextView) vi.findViewById(R.id.serverDistanceTextView);
	        TextView lastTV = (TextView) vi.findViewById(R.id.serverLastTextView);
	        
	        Typeface font = Typeface.createFromAsset(activity.getAssets(), "font/capt.ttf");
	        nameTV.setTypeface(font);
	        stateTV.setTypeface(font);
	        distTV.setTypeface(font);
	        lastTV.setTypeface(font);
	        
            holder = new ViewHolder();
	        holder.name = nameTV;
	        holder.state = stateTV;
	        holder.dist = distTV;
	        holder.last = lastTV;
	        vi.setTag( holder );
	     }
	     else {
	         holder=(ViewHolder)vi.getTag();
	     }
	      
	     if(data.size()>0)
	     {
	         tempValues = ( Server ) data.get( position );
	         
	         if (position == mSelectedItem) {
	        	 vi.setBackgroundColor(res.getColor(R.color.bluePale));
	         }
	         else{
	        	 vi.setBackgroundColor(res.getColor(R.color.blue));
	         }
	         
    		//instanciation object
	         holder.name.setText( tempValues.getName());
	         if(tempValues.isOnline()){
	        	 holder.state.setText(res.getString(R.string.online));
	         }
	         else{
	        	 holder.state.setText(res.getString(R.string.offline));
	         }
             holder.dist.setText(String.valueOf(Tools.getDistance(tempValues.getLocalisation(), Tools.getCurrentPosition()))+" meters");
             holder.last.setText( Tools.convertDateToString(tempValues.getLastAccessDate()));
  
             vi.setOnClickListener(new OnItemClickListener(position));
             vi.setOnLongClickListener(new OnItemLongClickListener(position));
	     }
	     else{
	    	 holder.name.setText("");
	         holder.state.setText("");
             holder.dist.setText("");
             holder.last.setText("");
             vi.setBackgroundColor(res.getColor(R.color.black));
	     }
	     return vi;
	 }
	          	  
     private class OnItemClickListener  implements OnClickListener{           
    	 private int mPosition;
              
         OnItemClickListener(int position){	 
             mPosition = position;
         }       
          
         @Override
         public void onClick(View arg0) {
        	 DestinationServerActivity sct = (DestinationServerActivity)activity;
        	 sct.onServerItemClick(mPosition);
        	 mSelectedItem = mPosition;
        	 notifyDataSetChanged();
        }              
     }
     
     private class OnItemLongClickListener  implements OnLongClickListener{           
    	 private int mPosition;
              
         OnItemLongClickListener(int position){	 
             mPosition = position;
         }

		@Override
		public boolean onLongClick(View v) {
			DestinationServerActivity sct = (DestinationServerActivity)activity;
       	 	if(mSelectedItem == mPosition){
       	 		mSelectedItem=-1;
       	 		DestinationServerActivity.nextButton.setEnabled(false);
       	 	}
       	 	if(mSelectedItem>mPosition){
       	 		mSelectedItem=mSelectedItem-1;
       	 	}
       	 	
       	    sct.onServerItemLongClick(mPosition);
			notifyDataSetChanged();
			return false;
		}  
         
     }

	@Override
	public void onClick(View v) {
		
	}   
	
	public int getSelectedItem(){
		return mSelectedItem;
	}
	
	public void setSelectedItem(int select){
		mSelectedItem = select;
	}	
	
	
}